<?php

namespace Admin\Controller;

class TagController extends BaseController {

    public function _initialize() {
        $this->module = '文章';
        $this->module_en = 'Tag';
        parent::_initialize();
    }

    /**
     * 标签添加
     */
    public function insert() {
        parent::insert();
    }

    /**
     * 添加页面
     */
    public function add() {
        parent::add();
    }

    /**
     * 标签列表
     */
    public function lists() {
        $model = D('Tag');
        $page = I('page', 1, 'intval');
        $this->assign('cur_crumb', array(array('name' => '标签')));
        $this->assign('page_title', '标签');
        $this->model = $model->page($page, \Think\Constant::PAGESIZE)->limit(\Think\Constant::PAGESIZE)->order('tag_id desc');
        parent::lists();
    }

    public function getExtendData($act = 'insert') {
        if ($act == 'insert' && IS_AJAX) {
            $array['content'] = $this->ajaxList();
        } else {
            $array = array();
        }
        return $array;
    }

    /**
     * 标签编辑
     */
    public function modify() {
        $post = I('post.');
        $post['tag_id'] = intval($post['tag_id']);
        empty($post['tag_id']) && $this->error('编辑失败');
        parent::modify($post);
    }

    public function edit() {
        $tag_id = I('get.tag_id', 0, 'intval');
        empty($tag_id) && $this->error('页面不存在');
        $this->assign('cur_crumb', array(array('name'=>'标签','link'=>U('Tag/lists')),array('name' => '标签编辑')));
        $this->assign('page_title', '标签编辑');
        $this->assign('action_link', array(array('name' => '标签', 'link' => U('Tag/lists'))));
        $this->model = M('tag');
        $this->query_condition = array('tag_id' => $tag_id);
        parent::edit();
    }

    /**
     * 搜索
     */
    public function search() {
        $post = I('post.');
        if ($post['sort_field']) {
            $order = $post['sort_field'] . ' ' . getSortType($post['sort_type'], isset($post['sort_reverse']) ? false : true);
        }
        $content = $this->ajaxList(isset($map) ? $map : array(), $order);
        $this->ajaxReturn(array('info' => '查询成功', 'status' => 1, 'content' => $content));
    }

    /**
     * 删除
     */
    public function delete() {
        $tag_id = I('get.tag_id', 0, 'intval');
        empty($tag_id) && $this->ajaxReturn(array('删除失败！', 'status' => 0));
        $this->search();
    }

    /**
     * 移入回收站
     */
    public function del() {
        
    }

    protected function ajaxList($map = array(), $order = 'tag_id desc') {
        $model = D('Tag');
        $page = I('page', 1, 'intval');
        list($sort_field, $sort_type) = explode(' ', $order);
        $this->assign('sort_field', $sort_field)->assign('sort_type', $sort_type);
        $this->assign('list', $model->page($page, 15)->limit(15)->order($order)->loadList($map));
        $this->assign('ajax', 1);
        $content = $this->fetch('_list');
        return $content;
    }

}
